import ast
from typing import Literal, Type, Union
from pydantic import BaseModel
import numpy as np


def maybe_complex_list_serialize(arr: Union[list[complex, int, float], np.ndarray]) -> str:
    if isinstance(arr, np.ndarray):
        return str(arr.tolist())
    return str(arr)


def maybe_complex_list_deserialize(arr: str) -> list[Union[complex, int, float]]:
    return ast.literal_eval(arr)


def make_literal(cls: Union[Type[BaseModel], dict, list]) -> Type[str]:
    if isinstance(cls, dict):
        return Literal[tuple(cls.keys())]

    if isinstance(cls, list):
        return Literal[tuple(cls)]

    return Literal[tuple(cls.__fields__)]
